var u = navigator.userAgent;
// var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
// var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
var isRelease = true;

//===================== 测试数据 =====================
// var uID = "8e84c053-7a31-41c7-9296-7e8ecc87c378";
var uID = "7203423d-3d12-4272-9957-0ecc4757c8c5";
// var sLOGIN = "95445a567cc43c75733be30792b4a901cc1995f8";
var sLOGIN = "d3c63dd6f690aa2867c094765102c9f69ff7fb32";
var userType = "14";
//===================== 测试数据 =====================

/**
 * 打印日志
 * @param {Object} msg
 */
function log(msg) {
  console.log(msg);
}
// 原生日志打印
function logNative(msg) {
  console.log(msg);
  dsBridge.call("base.logNative", msg);
}


/**
 * 显示加载中
 * @param msg 
 * @param shadeClose  是否点击遮罩时关闭层 默认true
 */
function loading(msg, shadeClose) {
  if (isEmpty(msg))
    msg = "加载中";
  //loading带文字
  if (shadeClose != false) {
    shadeClose = true;
  }
  layer.open({
    type: 2,
    content: msg,
    shadeClose: shadeClose
  });


}

/**
 * Toast
 * @param {Object} msg  提示语
 * @param {Object} type 提示类型
 */
function showToast(msg) {

  layer.open({
    content: msg,
    skin: 'msg',
    time: 2 //2秒后自动关闭
  });
}

function getServer() {
  if (isRelease)
    return dsBridge.call("base.getServer", "")
  else
    return url;
}

// 显示提示框...
function showAlert(msg) {
  //信息框
  layer.open({
    content: msg,
    btn: '确定'
  });
}
// 显示提示框...点击后关闭当前页
function showAlertClose(msg) {
  //信息框
  layer.open({
    content: msg,
    btn: '确定',
    yes: function (index) {
      layer.close(index);
      close();
    }
  });
}

// 关闭当前页面
function close() {
  return dsBridge.call("base.closeJs", "");
}
// 显示加载框...
function showDialog(msg) {
  dsBridge.call("base.showDialogJs", msg);
}
// 取消加载框...
function cancelDialog() {
  dsBridge.call("base.cancelDialogJs", "");
}
// 获取LOGIN
function getLOGIN() {
  if (isRelease)
    return dsBridge.call("base.getLOGINJs", "");
  else
    return uID;
}
// 获取SECURELOGIN
function getSECURELOGIN() {
  if (isRelease)
    return dsBridge.call("base.getSECURELOGINJs", "");
  else
    return sLOGIN;
}
// 获取getUserId
function getUserId() {
  if (isRelease)
    return dsBridge.call("base.getUserId", "");
  else
    return uID;
}
// 用户类型getUserType
function getUserType() {
  if (isRelease)
    return dsBridge.call("base.getUserTypeJs", "");
  else
    return userType;
}
// 审核状态getAuditStatus
function getAuditStatus() {
  if (isRelease)
    return dsBridge.call("base.getAuditStatusJs", "");
  else
    return auditStatus;
}

/**
 * JSON对象转化为JSON字符串
 * @param {Object} data
 */
function toJson(data) {
  return JSON.stringify(data);
}

/**
 * 字符串转json对象
 * @param {Object} str
 */
function strToJson(str) {
  return eval('(' + str + ')');
}

/**
 * 小图url
 * @param {Object} imgName
 */
function showSmallImg(imgName) {
  return url_show_image + imgName + "_s";
}

/**
 * 中图url
 * @param {Object} imgName
 */
function showMidImg(imgName) {
  return url_show_image + imgName + "_m";
}

/**
 * 大图url
 * @param {Object} imgName
 */
function showBigImg(imgName) {
  return url_show_image + imgName + "_c";
}



/**
 * js过滤html标签
 * @param {Object} str
 */
function removeHTMLTag(str) {
  str = str.replace(/<\/?[^>]*>/g, '');
  //去除HTML tag
  str = str.replace(/[ | ]*\n/g, '\n');
  //去除行尾空白
  //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
  str = str.replace(/ /ig, '');
  //去掉
  return str;
}


// 获取字符串长度
function strlen(str) {
  var len = 0;
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    //单字节加1
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      len++;
    } else {
      len += 2;
    }
  }
  return len;
}

function getDistance(lat1, lng1, lat2, lng2) {

  var radLat1 = Rad(lat1);
  var radLat2 = Rad(lat2);
  var a = radLat1 - radLat2;
  var b = Rad(lng1) - Rad(lng2);
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137;
  // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  //输出为公里
  s = s.toFixed(1);
  return s;
}

function Rad(d) {
  return d * Math.PI / 180.0;
  //经纬度转换成三角函数中度分表形式。
}

//判断字符是否为空
function isEmpty(obj) {
  if (typeof obj == "undefined" || obj == null || obj == "" || obj == "null") {
    return true;
  } else {
    return false;
  }
}


/*
 * text  要过滤的文本
 * arrMg 过滤关键字数组  例如 var arrMg = ["fuck", "tmd", "他妈的"];等敏感词过滤
 */
function filter(text, arrMg) {
  var showContent = text;
  // 正则表达式
  // \d 匹配数字
  for (var i = 0; i < arrMg.length; i++) {
    // 创建一个正则表达式
    var r = new RegExp(arrMg[i], "ig");
    showContent = showContent.replace(r, "*");
  }
  // 显示的内容--showInput
  return showContent;
}



/**
 *获取最近7天日期
 *getDay(0) 当天日期
 *getDay(-7) 7天前日期
 *getDay(7) 7天后日期
 */
function getDay(day) {
  var today = new Date();

  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;

  today.setTime(targetday_milliseconds);
  //注意，这行是关键代码
  // 目前项目没用到年 这里屏蔽掉
  //var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tMonth + "-" + tDate;
}

function doHandleMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

//获取当前时间，格式YYYY-MM-DD
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

/**
 * 根据日期字符串获取星期几
 * @param dateString 日期字符串（如：2016-12-29），为空时为用户电脑当前日期
 * @returns {String}
 */
function getWeek(dateString) {
  var dateArray = dateString.split("-");
  var date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
  return "星期" + "日一二三四五六".charAt(date.getDay());
};
/**
 *获取当前日期的后几天时间
 * @param {Object} AddDayCount
 */
function GetDateStr(AddDayCount) {
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount);
  //获取AddDayCount天后的日期
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;
  //获取当前月份的日期
  var d = dd.getDate();
  if (m < 10) {
    m = "0" + m;
  }
  if (d < 10) {
    d = "0" + d;
  }
  return y + "-" + m + "-" + d;
}

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
function get_thousand_num(num) {
  num = num.toString().replace(/\$|\,/g, '');
  if (isNaN(num))
    num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  cents = num % 100;
  num = Math.floor(num / 100).toString();
  if (cents < 10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    num = num.substring(0, num.length - (4 * i + 3)) + ',' +
      num.substring(num.length - (4 * i + 3));
  return (((sign) ? '' : '-') + num + '.' + cents);
}


// 校验手机号
function checkPhone(phone) {
  if (!(/^1\d{10}$/.test(phone))) {
    showToast("手机号码有误，请重填");
    return false;
  }
  return true;
}

//校验联系方式为座机号或手机号
function checkMobilePhone(phone) {
  var tel = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
  var mobile = /^((\+?86)|(\(\+86\)))?(13[0123456789][0-9]{8}|14[012356789][0-9]{8}|15[012356789][0-9]{8}|17[012356789][0-9]{8}|18[0123456789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
  var result = (mobile.test(phone) || tel.test(phone));
  return result;
}

// 校验身份证
var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
function checkIDCard(idNo) {
  if (!regIdNo.test(idNo)) {
    showToast('身份证号填写有误');
    return false;
  }
  return true;
}

//格式化日期
function formatDateBase(dateString, formatString) {
  if (isEmpty(dateString)) {
    return '';
  }
  if (dateString == undefined || dateString == null || dateString == '') {
    dateString = new Date(); //空值则取当前时间
  }
  dateString = new Date(dateString);
  if (dateString == "Invalid Date") { //对时间扩展new Date().toPrettyDateString("yyyy-MM-dd")这里没关系，不会有问题
    dateString = new Date(); //但是如果直接用prettyDateString("sdfg")作为函数使用可以对他进行判断验证
  }
  var year = dateString.getFullYear();
  var month = addDate(dateString.getMonth() + 1); //month从0开始记所以加1
  var day = addDate(dateString.getDate());
  var hour = addDate(dateString.getHours());
  var minute = addDate(dateString.getMinutes());
  var second = addDate(dateString.getSeconds());
  var result = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  if (formatString == null) {
    return result;
  };
  result = formatString.replace(/yyyy/, year).replace(/MM/, month).replace(/dd/, day).replace(/HH/, hour).replace(/mm/, minute).replace(/ss/, second);
  return result;
}

function addDate(inputIntValue) {
  if (inputIntValue < 10) {
    inputIntValue = "0" + inputIntValue;
    //因 月 日 时  分 秒 会存在只有个位数字，这样加0稍微美观一点
    //用2000-01-01 01:02:02 不会出现 2000-1-1 1:2:2
  }
  return inputIntValue;
}

//限制只能保留两位小数点
function priceCheck(obj) {
  obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
  obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
  obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
  obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
}

// 设置localStorage
function setVal(name, value) {
  localStorage[name] = value
}
// 获取localStorage
function getVal(name) {
  return localStorage[name]
}

//判空展示
function emptyShow(val) {
  return isEmpty(val) ? "" : val;
}